// 批量替换字体脚本
const fs = require('fs');
const path = require('path');
const glob = require('glob');

// 需要替换的字体模式
const fontPatterns = [
  // 替换所有 Georgia, serif
  { from: /font-family:\s*['"]Georgia['"]\s*,\s*serif;?/g, to: 'font-family: \'Fugaz One\', \'PingFang SC\', \'Microsoft YaHei\', sans-serif;' },
  // 替换所有 PingFang SC, Microsoft YaHei
  { from: /font-family:\s*['"]PingFang SC['"]\s*,\s*['"]Microsoft YaHei['"]\s*,\s*sans-serif;?/g, to: 'font-family: \'Fugaz One\', \'PingFang SC\', \'Microsoft YaHei\', sans-serif;' },
  // 替换所有 system-ui
  { from: /font-family:\s*system-ui;?/g, to: 'font-family: \'Fugaz One\', \'PingFang SC\', \'Microsoft YaHei\', sans-serif;' },
  // 替换所有 -apple-system
  { from: /font-family:\s*-apple-system;?/g, to: 'font-family: \'Fugaz One\', \'PingFang SC\', \'Microsoft YaHei\', sans-serif;' },
  // 替换所有 BlinkMacSystemFont
  { from: /font-family:\s*BlinkMacSystemFont;?/g, to: 'font-family: \'Fugaz One\', \'PingFang SC\', \'Microsoft YaHei\', sans-serif;' }
];

// 需要处理的文件类型
const filePatterns = [
  'pages/**/*.vue',
  'components/**/*.vue',
  'static/**/*.css',
  'App.vue'
];

console.log('开始批量替换字体...');

filePatterns.forEach(pattern => {
  const files = glob.sync(pattern);
  console.log(`处理模式: ${pattern}, 找到 ${files.length} 个文件`);
  
  files.forEach(filePath => {
    try {
      let content = fs.readFileSync(filePath, 'utf8');
      let hasChanges = false;
      
      // 应用所有字体替换规则
      fontPatterns.forEach(({ from, to }) => {
        const newContent = content.replace(from, to);
        if (newContent !== content) {
          content = newContent;
          hasChanges = true;
        }
      });
      
      // 如果有变化，写回文件
      if (hasChanges) {
        fs.writeFileSync(filePath, content, 'utf8');
        console.log(`✅ 已更新: ${filePath}`);
      }
    } catch (error) {
      console.error(`❌ 处理文件失败: ${filePath}`, error.message);
    }
  });
});

console.log('字体替换完成！');
